try
Type
control structure
Summary
Executes a list of statements, sending any errors to the try structure to be handled.
Syntax
try
<statementList>
[ catch <errorVariable>
<errorStatementsList> ]
[ finally
<cleanupStatementsList> ]
end try
Description
Use the try control structure to execute a series of statements and handle any error messages in the catch section, instead of letting LiveCode display the error window.
Form: The try control structure begins with the word try on a single line, followed by a list of LiveCode statements.
The catch section begins with the catch keyword, followed by a parameter name. If any errors occur during execution of the statementList, the error message is placed in the errorVariable parameter, and the errorStatementsList is executed. statements in the errorStatementsList can refer to the value of the errorReport.
The catch section may be followed by an optional finally section. The cleanupStatementsList is executed normally, even if LiveCode encountered an exit or pass statement in the statementList.
The try structure ends with an end try statement.
Each of the statements in the statementList is executed normally, just as though the statementList were not in a try structure, except that any errors that would normally display an error window instead trigger the catch section. This happens even if the error is in another handler that's called from within the try structure.
The statements in the catch section are executed only if an error occurs. Only errors that would normally display the error window are handled by the catch section. If the error would not normally display the error window--for example, errors when opening a file with the open file command --it doesn't trigger the catch section.
The statements in the finally section are executed whether or not there is an error. Since the finally section is always executed even if the statementList contains an exit or pass statement, it can be used for final cleanup actions such as deleting variables. The finally section is an optional part of the try structure.
The try control structure is implemented internally as a command and appears in the commandNames.
Parameters
Name | Type | Description |
---|---|---|
statementList | The statementList, errorStatementsList, and cleanupStatementsList each consist of one or more valid LiveCode statements. | |
errorVariable | A valid variable name. | |
errorStatementsList | ||
cleanupStatementsList |
Related
keyword: file, end try, finally, catch
command: open file
control structure: pass, exit, try, throw
function: value, commandNames
glossary: error message, variable, call, execute, command, control structure, error, trigger, keyword, parameter, statement, handler
Compatibility and Support
Introduced
LiveCode 1.0
OS
mac
windows
linux
ios
android
Platforms
desktop
server
mobile